import request from '@/utils/request'
import { Loading } from 'element-ui'
import store from '@/store'

export function fammterString(type, groupId) {
  let resString
  let schloolId = store.getters.schoolId
  let needIds = {
    user: store.getters.userId,
    branch: store.getters.branchId,
    school: store.getters.schoolId,
    group: groupId
  }
  //type类型user，branch，school,group
  resString = `cloud/netdisk/${schloolId}/${type}/${needIds[type]}`
  return resString
}

// 获取oss签名
export function getOssSignature(path) {
  return request({
    url: '/school/file/getOssSignature',
    method: 'get',
    params: {
      path: path
    }
  })
}

export function upload(data) {
  return new Promise((resolve, reject) => {
    const loading = Loading.service({
      lock: true,
      text: `正在上传,请稍等…(0%)`,
      spinner: 'el-icon-loading',
      background: 'rgba(255, 255, 255, 0.6)'
    })
    request({
      url: '/school/common/upload',
      method: 'post',
      data: data,
      onUploadProgress(e) {
        if (e.lengthComputable) {
          let p =
            e.loaded / e.total == 1
              ? '99'
              : parseInt((e.loaded / e.total) * 100)
          loading.setText(`正在上传,请稍等…(${p}%)`)
        }
      }
    })
      .then((res) => {
        loading.setText(`正在上传,请稍等…(100%)`)
        loading.close()
        resolve(res)
      })
      .catch((err) => {
        loading.close()
        reject(err)
      })
  })
}
